package com.oceantrader.sale.biz.hello.service;

import java.util.List;

import org.springframework.stereotype.Component;

import com.oceantrader.sale.biz.hello.dao.HelloDao;
import com.oceantrader.sale.biz.hello.entity.Hello;
import com.oceantrader.root.tools.Dao;

import io.vertx.core.Future;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class HelloService {
    private final Dao dao;
    private final Dao dao2;
    private final HelloDao studentDao;

    /** 新增Hello */
    public Future<Long> add(Hello param) {
        // var tx = dao.getTransaction();
        // return studentDao.addHello(tx, param)
        // .onSuccess(handler -> dao.commit(tx))
        // .onFailure(handler -> dao.rollback(tx));
        var tx = dao2.getTransaction();
        return studentDao.addHello(tx, param)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 删除Hello */
    public Future<Integer> del(List<Long> idList) {
        var tx = dao.getTransaction();
        return studentDao.delHello(tx, idList)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 修改用户 */
    public Future<Integer> set(Hello param) {
        var tx = dao.getTransaction();
        return studentDao.updateNullById(tx, param)
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }

    /** 查询Hello List */
    public Future<List<Hello>> getList(Hello param) {
        var tx = dao.getConnection();
        var helloList1 = studentDao.getHelloList(tx, param);
        var helloList2 = studentDao.getHelloList(tx, param);
        return Future.all(helloList1, helloList2)

                .map(mapper -> {
                    List<Hello> resultList = helloList1.result();
                    resultList.addAll(helloList2.result());
                    return resultList;
                })
                .onSuccess(handler -> dao.commit(tx))
                .onFailure(handler -> dao.rollback(tx));
    }
}
